﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CK.Plugin;
using CK.Kernel.Config;
using System.Collections;
using CommonServices;

namespace GesturePlugin
{
    [PluginEditor(Id = "{E456B7AE-1CA3-4931-92BB-34F189D4FE68}",
        PublicName = "Gesture Editor",
        EditorOf = "{E1FF5C6B-3D8D-4d7f-B4C6-7838AEDFA238}",
        EditableObjectTypes = new string[] { "User" },
        Version = "1.0.0",
        Categories = new string[] { "Editors" })]

    public partial class GestureEditorPlugin : IPluginEditor
    {
        /// <summary>
        /// Access to the property bag
        /// </summary>
        public IPluginConfigAccessor EditedConfiguration { get; set; }

        /// <summary>
        /// The default precision to have an available segment.
        /// </summary>
        public int MinimalDistanceOfCapture
        {
            get
            {
                object o = EditedConfiguration.User["MinimalDistanceOfCapture"];
                return o == null ? 50 : (int)o;
            }

            set { EditedConfiguration.User["MinimalDistanceOfCapture"] = value; }
        }

        /// <summary>
        /// Gestures' dictionnary
        /// </summary>
        public IList GestureDictionnary
        {
            get
            {
                object o = EditedConfiguration.User["gestureDictionnary"];
                return o == null ? new ArrayList() : (IList)o;
            }
        }

        #region IPlugin Membres

        public bool CanStart(out string lastError)
        {
            throw new NotImplementedException();
        }

        public bool Setup(ISetupInfo info)
        {
            throw new NotImplementedException();
        }

        public void Start()
        {
            throw new NotImplementedException();
        }

        public void Stop()
        {
            throw new NotImplementedException();
        }

        public void Teardown()
        {
            throw new NotImplementedException();
        }

        #endregion

        /// <summary>
        /// Creates the panel that is displayed when it was decided to draw a gesture
        /// </summary>
        /// <param name="editableObjectTypes"></param>
        /// <param name="o"></param>
        /// <returns>the panel</returns>
        public Panel CreatePanel(string[] editableObjectTypes, object o)
        {
            Panel p = new Panel() { AutoSize = true };
            GestureUserControl u = new GestureUserControl(this);

            p.Controls.Add(u);

            return p;
        }

        /// <summary>
        /// Saves a gesture to the dictionnary
        /// </summary>
        /// <param name="gestureName">The key in the dictionnary</param>
        /// <param name="_gestureString">The gesture pattern</param>
        public void SaveGesture(string gestureName, string _gestureString)
        {
            IList gestureDictionnary = (IList)EditedConfiguration.User["gestureDictionnary"];
            if (gestureDictionnary == null)
            {
                gestureDictionnary = new ArrayList();
            }
            bool alreadyExist = false;
            Gesture gestureWhereToAdd = new Gesture();

            for (int i = 0; i < gestureDictionnary.Count; i++)
            {
                Gesture gesture = (Gesture)gestureDictionnary[i];

                if (gestureName == gesture.Name)
                {
                    alreadyExist = true;
                    gestureWhereToAdd = gesture;
                }
            }

            if (alreadyExist)
            {
                gestureWhereToAdd.AddPattern(_gestureString);
                gestureDictionnary.Remove(gestureWhereToAdd);
            }
            else
            {
                gestureWhereToAdd = new Gesture(gestureName);
                gestureWhereToAdd.AddPattern(_gestureString);
            }

            gestureDictionnary.Add(gestureWhereToAdd);
          
            EditedConfiguration.User["gestureDictionnary"] = gestureDictionnary;
        }
    }
}
